package com.ebarlas.fractal;

public class MandelbrotUtil {
    
    public static Fractal mandelbrot() {
        Fractal fractal = new Fractal();
        fractal.setBase("0");
        fractal.setRecurrence("mbrot(c, z)");
        fractal.setRed("if(n == 50, 0, n * 11)");
        fractal.setGreen("if(n == 50, 0, n * 11)");
        fractal.setBlue("if(n == 50, 0, n * 11)");
        fractal.setHalt("abs(z) > 2.0 || n == 50");
        fractal.setRegionX(-2.0);
        fractal.setRegionY(-2.0);
        fractal.setRegionWidth(4.0);
        fractal.setRegionHeight(4.0);
        return fractal;
    }
    
    public static FractalTransition mandelbrotIterTransition() {
        FractalTransition ft = new FractalTransition();
        ft.setBase("0");
        ft.setRecurrence("mbrot(c, z)");
        ft.setRed("if(n >= (30/t)*f, 0, n * 11)");
        ft.setGreen("if(n >= (30/t)*f, 0, n * 11)");
        ft.setBlue("if(n >= (30/t)*f, 0, n * 11)");
        ft.setHalt("abs(z) > 2.0 || n >= (30/t)*f");
        ft.setRegionX("-2.0");
        ft.setRegionY("-2.0");
        ft.setRegionWidth("4.0");
        ft.setRegionHeight("4.0");
        return ft;
    }    

    public static int iterations(double cRe, double cIm, int maxIters) {
        int i=0;
        double zRe = 0;
        double zIm = 0;
        while(i<maxIters && zRe*zRe + zIm*zIm <= 4.0) {
            i++;
            double reTmp = zRe;
            zRe = zRe*zRe - zIm*zIm + cRe;
            zIm = 2 * reTmp * zIm + cIm;
        }
        return i;
    }
    
}
